package com.yazi.mxz.common;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.yazi.mxz.common.entity.beans.Member;

import com.myframework.smhj.common.tool.CommonUtil;

public class MyUtil {
	/**
	 * 密码加密
	 * @param src
	 * @return
	 */
	public static String encodePwd(String src) {
		return CommonUtil.md5Encode(src);
	}
	
	/**
	 * 用户安全问题（答案）加密
	 * @param src
	 * @return
	 */
	public static String encodeSecurityAnswer(String src) {
		return CommonUtil.md5Encode(src);
	}
	
	/**
	 * 用户密码加密
	 * @param member
	 * @return
	 */
	public static Member encodePwd(Member member) {
		int seed = new Double(Math.floor(Math.random()*99999999)).intValue();
		member.setSeed(seed);
		member.setPassword(CommonUtil.md5Encode(member.getRegisterTime() + member.getPassword() + seed));
		member.setPassword(CommonUtil.md5Encode(member.getName() + member.getPassword()));
		return member;
	}
	
	/**
	 * 检查密码是否为用户密码
	 * @param member 完整的用户对象
	 * @param pwd 要检查的密码
	 * @return
	 */
	public static boolean checkPwd(Member member, String pwd) {
		int seed = member.getSeed();
		pwd = CommonUtil.md5Encode(member.getRegisterTime() + pwd + seed);
		pwd = CommonUtil.md5Encode(member.getName() + pwd);
		if (!pwd.equals(member.getPassword())) {
			return false;
		}
		return true;
	}
	
	public static int integral(float cost) {
		// TODO 消费与积分比例
		return new Double(cost / 10).intValue();
	}
	
	public static int makePIN() {
		return (int) (Math.random()*9000 + 1000);
	}
	
	/**
	 * 生成验证码
	 * @return
	 */
	public static int makeCaptcha() {
		return (int) (Math.random()*899999 + 100000);
	}
	
	/**
	 * 生出随机密码
	 * @param length 密码长度
	 * @return
	 */
	public static String makePassword(int length) {
		return CommonUtil.randomLN(length);
	}
	
	/**
	 * 根据序列生成店内码
	 * @param seq
	 * @return
	 */
	public static String makeBarCode(int seq) {
		String barCode = "" + seq;
		int len = 5 - barCode.length();
		for (int i = 0; i < len; i++ ) {
			barCode = "0" + barCode;
		}
		return barCode;
	}
	
	/**
	 * 校验条码是否有效
	 * @param barCode
	 * @return
	 */
	public static boolean checkBarCode(String barCode) {
		char[] c = barCode.toCharArray();
		// 店内码
		if (c.length == 5) {
			return true;
		}
		// 国际零售商品条码
		return c.length == 13 && Integer.parseInt("" + c[c.length -1]) == getCheckBarCode(barCode);
	}
	
	
	public static Long makeOrderCode(long timestamp) {
		//1417414444302
		//timestamp = (timestamp - 1413201426901L);
		//timestamp = (15 - ("" + timestamp).length()) + Math.random() * ;
		return timestamp;
	}
	
	/**
	 * 生成店铺商品销售流水号
	 * @param storeId
	 * @return
	 */
	public static String makeSerialCode(long storeId) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
		int rd = (int)(Math.random()*900000000 + 10000000);
		// 8num-yyMMddhhmmss-random;
		return sdf.format(new Date()) + "-" + CommonUtil.jashEncode("" + (rd + storeId), 8) + "-" + rd;
	}
	
	/**
	 * 检查店铺商品销售流水号
	 * @param storeId
	 * @param serialCode
	 * @return
	 */
	public static boolean checkSerialCode(long storeId, String serialCode) {
		String[] strs = serialCode.split("_");
		if (CommonUtil.jashEncode("" + (strs[2] + storeId), 8) == Integer.parseInt(strs[1])) {
			return true;
		}
		return false;
	}
	
	/**
	 * 小数位取舍模式：将指定小位后的数字直接舍去
	 * @param num 要进行取舍的数字
	 * @param scale 保留小数位
	 * @return
	 */
	public static BigDecimal numRoundDown(float num, int scale) {
		return new BigDecimal("" + num).setScale(scale,BigDecimal.ROUND_DOWN);
	}
	
	/**
	 * 小数位取舍模式：将指定小位后的数字直接舍去
	 * @param num 要进行取舍的数字
	 * @param scale 保留小数位
	 * @return
	 */
	public static BigDecimal numRoundDown(double num, int scale) {
		return new BigDecimal("" + num).setScale(scale,BigDecimal.ROUND_DOWN);
	}
	
	/**
	 * 计算出条码的校验码
	 * @param barCode
	 * @return
	 */
	private static int getCheckBarCode(String barCode) {
		char[] c = barCode.toCharArray();
		int check = 0;
		int odd = 0;
		int even = 0;
		for (int i = c.length - 2; i >= 0; i -= 2) {
			odd += Integer.parseInt("" + c[i]);
		}
		for (int i = c.length - 3; i >= 0; i -= 2) {
			even += Integer.parseInt("" + c[i]);
		}
		check = odd * 3 + even;
		return check / 10 * 10 + (check % 10 == 0 ? 0 : 10) - check;
	}
}
